<?php

namespace app\api\controller;
use app\api\common\Common;

class Index extends Common {
    
    /**
     * 小程序首页数据
     * @return [json]      [小程序首页数据]
     */
    public function index(){
        $dataurl = $this->__DATAURL__;
        // 如果缓存首页没有数据，那么就读取数据库
        // 获取首页顶部轮播图（5张图）
        $ggtop = db('guanggao')->field('id, name, photo')->order('sort desc, id asc')->limit(5)->select();
        foreach ($ggtop as $k => $v){
            $ggtop[$k]['photo'] = $dataurl.$v['photo'];
            $ggtop[$k]['name'] = urldecode($v['name']);
        }

        // 首页推荐品牌20个
        $brand = db('brand')->field('id, name, photo')->where('type = 1')->limit(20)->select();
        foreach ($brand as $k => $v){
            $brand[$k]['photo'] = $dataurl.$v['photo'];
        }

        // 首页推荐产品
        $pro_list = db('product')->where('del = 0 AND pro_type = 1 AND is_down = 0 AND type = 1')
                                 ->order('sort desc, id desc')
                                 ->field('id, name, intro, photo_x, price_yh, price, shiyong')
                                 ->limit(10)->select();
        foreach($pro_list as $k => $v){
            $pro_list[$k]['photo_x'] = $dataurl.$v['photo_x'];
        }

        $this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1], array('ggtop' => $ggtop, 'brand' => $brand, 'proList' => $pro_list));
    }

    /**
     * 小程序首页数据推荐产品数据分页
     * @return [json]      [推荐产品分页数据]
     */
    public function product_page(){
        $data = $this->params;
        $dataurl = $this->__DATAURL__;
        if (!isset($data['num'])) {
            $data['num'] = 10;
        }
        $begain = ($data['page'] - 1)*$data['num'];
        // 首页推荐产品
        $pro_list = db('product')->where('del = 0 AND pro_type = 1 AND is_down = 0 AND type = 1')
                                 ->order('sort desc, id desc')
                                 ->field('id, name, intro, photo_x, price_yh, price, shiyong')
                                 ->limit($begain, $data['num'])->select();
        foreach($pro_list as $k => $v){
            $pro_list[$k]['photo_x'] = $dataurl.$v['photo_x'];
        }
        
        $this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1], array('proList' => $pro_list));
    }
}